/**
 * 问题提报 - 编辑页面
 * @author liuw
 * @date 2019/10/25
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        current = 0,

        vueData = {
            isAdd: false,
            form: {
                ESAA001_NAME: null,
                ESAA010_NAME: null,
                ESAA005_NAME: null
            },
            imgs: g_ctxPath + 'upload/imgs',
            img: '',
            imgList: [],
            imgList2: [],

            files: g_ctxPath + 'upload/files',
            address: '',
            fileList: [],

            videoUrl: g_ctxPath + 'upload/files',
            videoList: [],
            oldVideoUrl: '',

            rules: {
                ESAA001: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                ESAA002: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                ESAA003: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                ESAA004: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                ESAA005: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                ESAA006: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                ESAA007: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                ESAA008: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
            },
            selectOptions: {
                sys_y_n: app.datas_('sys_y_n'),
                esaa003Options: app.datas_('problem_esai100_esaa003'),
                esaa005Options: app.datas_('problem_esai100_esaa005'),
                esaa006Options: app.datas_('problem_esai100_esaa006'),
                esaastusOptions: app.datas_('problem_esai100_esaastus')
            },
            count: 0,
            dialogImageUrl: '',
            dialogVisible: false,
            detailReadonly: true,
            userType: null
        },
        callObj,
        vue,
        moduleName,
        layOpenIndex
    ;

    var esai100Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'esai100_edit',
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },
        editData: function () {
            app.ajax({
                xurl: 'crm/esai100/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: moduleName + '信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        // callObj.update(result.xdata);
                        vueData.img = '';
                        vueData.imgList = [];
                        vueData.imgList2 = [];

                        vueData.fileList = [];
                        vueData.address = '';

                        vueData.videoList = [];
                        vueData.oldVideoUrl = '';

                        app.pageModule('esai100').reloadTableCurr();
                        layer.close(layOpenIndex);
                    }
                }
            });
        },
        addData: function () {
            app.ajax({
                xurl: 'crm/esai100/add',
                xdata: vueData.form,
                loading: true,
                okmsg: moduleName + '信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        vueData.img = '';
                        vueData.imgList = [];
                        vueData.imgList2 = [];

                        vueData.address = '';
                        vueData.fileList = [];

                        vueData.videoList = [];
                        vueData.oldVideoUrl = '';

                        app.pageModule('esai100').reloadTableCurr();
                        layer.close(layOpenIndex);
                    }
                }
            });
        },
        _call: function (tagData) {
            vue.$refs['form'].resetFields();
            layOpenIndex = tagData.layOpenIndex;
            moduleName = tagData.moduleName;
            vueData.img = '';
            vueData.imgList = [];
            vueData.imgList2 = [];

            vueData.address = '';
            vueData.fileList = [];

            vueData.videoList = [];
            vueData.oldVideoUrl = '';

            vueData.count = 0;
            if (tagData.is_add) {
                vueData.isAdd = true;
                vueData.detailReadonly = false;
                vueData.userType = '0';
                app.extendUpLow({
                    target: vueData.form,
                    res: {ESAASTUS: '1'},
                    reset: true
                });

                vueData.form.ESAA004 = new Date();
                // 获取默认数据
                app.ajax({
                    xurl: 'crm/esai100/getAddData',
                    cb: function (result) {
                        if (result.ok) {
                            let data = result.data.data;
                            vueData.form.ESAA001 = data.ESAA001;
                            vueData.form.ESAA001_NAME = data.ESAA001_NAME;
                            vueData.form.ESAA002 = data.ESAA002;
                            vueData.form.ESAA010 = data.ESAA010;
                            vueData.form.ESAA010_NAME = data.ESAA010_NAME;
                        }
                    }
                });
            } else {
                vueData.isAdd = false;
                callObj = tagData.obj;
                if (tagData.operation === 'detail') {
                    vueData.detailReadonly = true;
                } else {
                    vueData.detailReadonly = false;
                }
                vueData.userType = callObj.data.userType;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
                if (callObj.data.ESAA009 !== undefined) {
                    if (callObj.data.ESAA009.length > 0) {
                        let list = callObj.data.ESAA009.split(',');
                        for (let i = 0; i < list.length; i++) {
                            vueData.imgList.push({
                                name: list[i], url: '/wissun-smfile/' + list[i]
                            });
                            vueData.imgList2.push({ESAA009: list[i]});
                        }
                    }
                }
                if (callObj.data.ESAA012 !== undefined) {
                    if (callObj.data.ESAA012.length > 0) {
                        vueData.address = callObj.data.ESAA012;
                        vueData.fileList.push({
                            name: callObj.data.ESAA012, url: '/wissun-smfile/' + callObj.data.ESAA012
                        })
                    }
                }
                if (callObj.data.ESAA014 !== undefined && callObj.data.ESAA014 !== "") {
                    vueData.oldVideoUrl = callObj.data.ESAA014;
                    vueData.videoList.push({
                        name: callObj.data.ESAA014, url: '/wissun-smfile/' + callObj.data.ESAA014
                    })
                }
            }
        },
        _ready: function () {
            vueData.form = app.fields_(['ESAA_T'], vueData.form);
            vue = new Vue({
                el: '#esai100_edit',
                data: vueData,
                methods: {
                    cancel: function () {
                        layer.close(layOpenIndex);
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {

                                if (vueData.isAdd) { // 新增
                                    var flag = true;
                                    var flag = true;
                                    if (vue.$refs.upload2.uploadFiles.length > 0) {
                                        flag = false;
                                        vue.$refs.upload2.submit();
                                    }
                                    if (vue.$refs.upload3.uploadFiles.length > 0) {
                                        flag = false;
                                        vue.$refs.upload3.submit();
                                    } else {
                                        if (vue.$refs.upload1.uploadFiles.length > 0) {
                                            flag = false;
                                            vue.$refs.upload1.submit();
                                        }
                                    }
                                    if (flag) {
                                        self.addData();
                                    }
                                } else {  // 编辑
                                    vueData.form.ESAA009 = '';
                                    // 将未修改过的图片链接拼接起来
                                    let esaa009s = [];
                                    for (var i = 0; i < vueData.imgList2.length; i++) {
                                        esaa009s[i] = vueData.imgList2[i].ESAA009;
                                    }
                                    vueData.form.ESAA009 = esaa009s.join(",");

                                    // 将没有改动过的图片从上传列表中删掉
                                    vueData.imgList2.forEach(function (v) {
                                        vue.$refs.upload1.uploadFiles.removeByMap({
                                            name: v.ESAA009
                                        });
                                    });
                                    vue.$refs.upload2.uploadFiles.removeByMap({
                                        name: vueData.address
                                    });
                                    vue.$refs.upload3.uploadFiles.removeByMap({
                                        name: vueData.oldVideoUrl
                                    });
                                    var flag = true;
                                    if (vue.$refs.upload2.uploadFiles.length > 0) {
                                        flag = false;
                                        vue.$refs.upload2.submit();
                                    }
                                    if (vue.$refs.upload3.uploadFiles.length > 0) {
                                        flag = false;
                                        vue.$refs.upload3.submit();
                                    } else {
                                        if (vue.$refs.upload1.uploadFiles.length > 0) {
                                            flag = false;
                                            vue.$refs.upload1.submit();
                                        }
                                    }
                                    if (flag) {
                                        self.editData();
                                    }

                                }
                            }
                        });
                    },
                    openEsaa010: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_sysuser_id',
                                    columnList: [
                                        {label: '人员编号', prop: 'ID', width: '150'},
                                        {label: '姓名', prop: 'NAME', width: '150'},
                                        {label: '账户', prop: 'ACCOUNT', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.ESAA010 = data.ID;
                                    vueData.form.ESAA010_NAME = data.NAME;
                                },
                            },
                            index: 0,
                            openOpts: {
                                title: '选择处理人员',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    openEsaa005: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwde003',
                                    searchParam: {
                                        GWDE002: "产品线"
                                    },
                                    columnList: [
                                        {label: '选项值', prop: 'GWDE003', width: '150'},
                                        {label: '选项名称', prop: 'GWDE004', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.ESAA005 = data.GWDE003;
                                    vueData.form.ESAA005_NAME = data.GWDE004
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择ERP营运中心',
                                area: ['600px', '470px']
                            }
                        });
                    },

                    beforeUploadImg: function (file) {
                        const isJPG = (file.type === 'image/png' || file.type === 'image/jpeg'
                            || file.type === 'image/gif');
                        const isLt2M = file.size / 1024 / 1024 < 2;
                        if (!isJPG) {
                            this.$message.error("上传图片只能是 JPG|PNG|GIF 格式!");
                        }
                        if (!isLt2M) {
                            this.$message.error("上传图片大小不能超过 2MB!");
                        }

                        return isJPG && isLt2M;
                    },
                    successUploadImg: function (response) {
                        if (response.code === 200) {
                            if (vueData.form.ESAA009 === '' || vueData.form.ESAA009 == null) {
                                vueData.form.ESAA009 = response.data[0].name;
                            } else {
                                vueData.form.ESAA009 = vueData.form.ESAA009 + ',' + response.data[0].name;
                            }
                            vueData.count++;
                            // 确保文件已经全部上传完毕才执行新增/编辑方法
                            if (vueData.count === vue.$refs.upload1.uploadFiles.length) {
                                vue.$message.success('图片全部上传成功');
                                if (vueData.isAdd) {
                                    self.addData();
                                } else {
                                    self.editData();
                                }
                                vue.$refs.upload1.clearFiles();
                            }
                        }
                    },
                    handleImgPreview: function (file) {
                        vueData.dialogImageUrl = '/wissun-smfile/' + file.name;
                        vueData.dialogVisible = true;
                    },
                    handleJpgRemove: function (file, fileList) {
                        if (!vueData.isAdd) {
                            vueData.imgList2.removeByMap({
                                ESAA009: file.name
                            });
                            file = '';
                        }
                    },
                    handleExceedImg: function (files, fileList) {
                        this.$message.warning("当前只允许选择一个图片，如果需要选择新的图片，请先删除之前的图片!");
                    },

                    beforeUploadFiles: function (file) {
                        const isJPG = (file.type === 'application/pdf'
                            || file.type === 'text/plain'
                            || file.type === 'application/msword' || file.type === 'application/vnd.ms-excel'
                            || file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                            || file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif'
                            || file.type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
                        const isLt2M = file.size / 1024 / 1024 < 5;
                        if (!isJPG) {
                            this.$message.error('文件格式不对');
                        }
                        if (!isLt2M) {
                            this.$message.error('文件大小不对');
                        }
                        return isJPG && isLt2M;
                    },
                    successUploadFiles: function (response) {
                        if (response.code == 200) {
                            vue.$message.success('文件上传成功');
                            vueData.form.ESAA012 = response.data[0].name;
                            if (vue.$refs.upload3.uploadFiles.length > 0) {
                                vue.$refs.upload3.submit();
                            } else if (vue.$refs.upload1.uploadFiles.length > 0) {
                                vue.$refs.upload1.submit();
                            } else {
                                if (vueData.isAdd) {
                                    self.addData();
                                } else {
                                    self.editData();
                                }
                                vue.$refs.upload2.clearFiles();
                            }
                        }
                    },
                    handleFileExceed: function (files, fileList) {
                        this.$message.warning("只能选择一个文件，如果需要选择新的文件，请先删除之前的文件!");
                    },
                    handleFilePreview: function (file) {
                        window.open('/wissun-smfile/' + file.name);
                    },
                    handleFileRemove: function (file, fileList) {
                        vueData.form.ESAA012 = "";
                        file = '';
                    },
                    bigShow: function (item) {
                        new Viewer(document.getElementById('ESAA009_IMGS'));
                    },
                    // tranform1: function () { //左旋转
                    //     var img = document.getElementById('fullImg');
                    //     current = (current+90)%360;
                    //     img.style.transform = 'rotate('+current+'deg)';
                    // },
                    // tranform2: function () {//右旋转
                    //     var img = document.getElementById('fullImg');
                    //     current = (current-90)%360;
                    //     img.style.transform = 'rotate('+current+'deg)';
                    // },
                    // close: function () {
                    //     var fullCon = document.getElementById('imgFull');
                    //     fullCon.style.display = 'none';
                    // },

                    handleVideoSuccess: function (res, file) {
                        vueData.videoFlag = false;
                        vueData.videoUploadPercent = 0;
                        if (res.code == 200) {
                            vueData.form.ESAA014 = res.data[0].name;
                            if (vue.$refs.upload1.uploadFiles.length > 0) {
                                vue.$refs.upload1.submit();
                            } else {
                                if (vueData.isAdd) {
                                    self.addData();
                                } else {
                                    self.editData();
                                }
                                vue.$refs.upload3.clearFiles();
                            }
                        } else {
                            this.$message.error('视频上传失败，请重新上传！');
                        }
                    },
                    handleVideoRemove: function (file, fileList) {
                        vueData.form.ESAA014 = "";
                    },
                    beforeUploadVideo: function (file) {
                        const isLt10M = file.size / 1024 / 1024 < 10;
                        if (['video/mp4', 'video/ogg', 'video/flv', 'video/avi', 'video/wmv', 'video/rmvb'].indexOf(file.type) == -1) {
                            this.$message.warning('请上传正确的视频格式');
                            return false;
                        }
                        if (!isLt10M) {
                            this.$message.warning('上传视频大小不能超过10MB!');
                            return false;
                        }
                    },
                },
                watch: {},
                created: function () {
                    self._setEleIsReady('esai100_edit');
                }
            });
        },
        _lazy: function () {
            this._super();
        }
    });
    return new esai100Edit();
});
